package cn.huiyunche.base.service.interfaces.impl;

import cn.huiyunche.base.service.constant.QiniuConstant;
import cn.huiyunche.base.service.framework.utils.HYCUtils;
import cn.huiyunche.base.service.framework.utils.QiniuUtils;
import cn.huiyunche.base.service.interfaces.BVehicleService;
import cn.huiyunche.base.service.mappers.BVehicleBrandMapper;
import cn.huiyunche.base.service.mappers.BVehicleMapper;
import cn.huiyunche.base.service.mappers.ext.BVehicleViewMapper;
import cn.huiyunche.base.service.model.BVehicle;
import cn.huiyunche.base.service.model.BVehicleBrand;
import cn.huiyunche.base.service.model.BVehicleBrandExample;
import cn.huiyunche.base.service.model.BVehicleExample;
import cn.huiyunche.base.service.vo.BVehicleBrandVo;
import cn.huiyunche.base.service.vo.BVehicleSearchVo;
import cn.huiyunche.base.service.vo.PageVo;
import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 车系接口类
 *
 * @author Administrator
 */
@Service

public class BVehicleServiceImpl implements BVehicleService {

    private static final Logger LOGGER = LoggerFactory.getLogger(BVehicleServiceImpl.class);

    @Autowired
    private BVehicleMapper bVehicleMapper;

    @Autowired
    private BVehicleViewMapper bVehicleViewMapper;

    @Autowired
    private BVehicleBrandMapper bVehicleBrandMapper = null;

    @Override
    public List<BVehicle> selectVehiclesByBrandId(String brandId) throws Exception {
        LOGGER.info("selectVehiclesByBrandId service param : {}", brandId);
        BVehicleExample search = new BVehicleExample();
        BVehicleExample.Criteria vehicles = search.createCriteria();
        vehicles.andBrandIdEqualTo(Long.parseLong(brandId));
        return bVehicleMapper.selectByExample(search);
    }

    @Override
    public List<BVehicle> selectVehiles() throws Exception {
        BVehicleExample search = new BVehicleExample();
        return bVehicleMapper.selectByExample(search);
    }

    @Override
    public BVehicle selectLikeName(String brandName, String vehicleName) {
        LOGGER.info("select vehicle name  like:{} ,result: {}", brandName, vehicleName);
        BVehicleExample search = new BVehicleExample();
        BVehicleExample.Criteria vehicles = search.createCriteria();
        vehicles.andVehicleNameLike(vehicleName);
        List<BVehicle> vehicleList = bVehicleMapper.selectByExample(search);
        if (vehicleList != null && vehicleList.size() > 0) {
            return vehicleList.get(0);
        }
        return null;
    }

    @Override
    public List<BVehicle> selectLikeBrandNameANDVehicleName(String brandKey, String vehicleKey) {
        LOGGER.info("selectLikeBrandNameANDVehicleName params : {}, {}", brandKey, vehicleKey);
        return bVehicleViewMapper.selectLikeBrandNameAndVehicleName(brandKey, vehicleKey);
    }

    @Override
    public BVehicle getById(long vehicleId) {
        LOGGER.info("getById param : {}", vehicleId);
        if (0 == vehicleId) {
            LOGGER.error("getByIdvehicleId must not be 0");
            throw new IllegalArgumentException("车型id不能是0");
        }
        return bVehicleMapper.selectByPrimaryKey(vehicleId);
    }

    @Override
    public List<BVehicleSearchVo> selectLikeBrandNameORVehicleName(String keystr) {
        LOGGER.info("selectLikeBrandNameORVehicleName param : {}", keystr);
        List<BVehicleSearchVo> voList = bVehicleViewMapper.selectLikeName(keystr, keystr);
        if (null != voList) {
            for (BVehicleSearchVo vo : voList) {
                String logokey = vo.getBrandLogo();
                vo.setBrandLogo(QiniuUtils.generateDownloadTicket(
                        HYCUtils.getConstantPropertiesLoader().getProperty(QiniuConstant.QINIU_LOGO_BUCKET),
                        logokey, "", "", ""));
            }

        }
        return voList;
    }

    @Override
    public BVehicle selctById(Long vehicleId) {
        LOGGER.info("selctById param : {}", vehicleId);
        return this.getBVehicleMapper().selectByPrimaryKey(vehicleId);
    }

    private BVehicleMapper getBVehicleMapper() {
        return this.bVehicleMapper;
    }

    private BVehicleViewMapper getBVehicleViewMapper() {
        return this.bVehicleViewMapper;
    }

    @Override
    public Map<String, Object> selectVehicleByParams(PageVo pageVo, BVehicleBrandVo vo) throws Exception {
        LOGGER.info("selectVehicleByParams params: {}, pages: {}", vo, pageVo);
        Map<String, Object> map = new HashMap<String, Object>();
        pageVo.setTotalRecord(this.getBVehicleViewMapper().selectVehicleCount(pageVo, vo));
        List<BVehicleBrandVo> list = this.getBVehicleViewMapper().selectVehicleByParams(pageVo, vo);
        if (CollectionUtils.isNotEmpty(list)) {
            for (BVehicleBrandVo bVehicleBrandVo : list) {
                String logokey = bVehicleBrandVo.getBrandLogo();
                bVehicleBrandVo.setBrandLogo(QiniuUtils.generateDownloadTicket(
                        HYCUtils.getConstantPropertiesLoader().getProperty(QiniuConstant.QINIU_LOGO_BUCKET),
                        logokey, "", "", ""));
            }

            LOGGER.info("selectVehicleByParams size: {}", list.size());
            map.put("vehicles", list);
            map.put("page", pageVo);
            return map;
        }
        return null;
    }

    @Override
    public int modify(BVehicle vehicle) throws Exception {
        Long id = vehicle.getId();
        if (id != null) {
            return this.getBVehicleMapper().updateByPrimaryKeySelective(vehicle);
        } else {
            return this.getBVehicleMapper().insert(vehicle);
        }
    }

    private BVehicleBrandMapper getBVehicleBrandMapper() {
        return this.bVehicleBrandMapper;
    }

    @Override
    public List<BVehicleBrand> loadBrand() throws Exception {
        return this.getBVehicleBrandMapper().selectByExample(new BVehicleBrandExample());
    }

    @Override
    public List<BVehicle> loadVehicle(Long brandId) throws Exception {
        BVehicleExample example = new BVehicleExample();
        example.createCriteria().andBrandIdEqualTo(brandId);
        return this.getBVehicleMapper().selectByExample(example);
    }

}
